package com.tanhua.backmanage.service;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.backmanage.vo.PageResultvo;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.api.VideoMassageApi;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.dubbo.server.vo.VideoMessageVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class VideoMassageService {

    @Reference(version = "1.0.0")
    private VideoMassageApi videoMassageApi;

    @Autowired
    private UserInfoService1 userInfoService1;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;


    public PageResultvo queryAllVideosList(Integer page, Integer pageSize, String sortProp, String sortOrder, String userid) {

        PageResultvo pageResult = new PageResultvo();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        Integer counts = videoMassageApi.getCounts(userid);
        Integer pages = counts%pageSize == 0 ? counts/pageSize : (counts/pageSize + 1);

        pageResult.setCounts(counts);
        pageResult.setPages(pages);




        PageInfo<Video> videoPageInfo = videoMassageApi.queryVideoList(userid , sortProp, sortOrder);
        List<Video> records = videoPageInfo.getRecords();

        if (CollUtil.isEmpty(records)) {
            return pageResult;
        }

        //查询用户信息
        List<Object> userIds = CollUtil.getFieldValues(records, "userId");
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.in("user_id", userIds);
        List<UserInfo> userInfoList = this.userInfoService1.queryUserInfoList(wrapper);
        UserInfo userInfo1 = userInfoList.get(0);



        Set<VideoMessageVo> videoVoList = this.createSet(sortProp,sortOrder);



        for (Video record : records) {

            VideoMessageVo videoMessageVo = new VideoMessageVo();

            videoMessageVo.setUesrId(Convert.toInt(record.getUserId()));
            videoMessageVo.setReportCount(0);
            videoMessageVo.setForwardingCount(0);
            videoMessageVo.setPicUrl(record.getPicUrl());
            videoMessageVo.setVideoUrl(record.getVideoUrl());
            videoMessageVo.setId(Convert.toInt(record.getVid()));
            videoMessageVo.setCreateDate(Convert.toInt(record.getCreated()));

            //Convert.toInt(this.quanZiApi.queryLikeCount(quanZiVo.getId()))
            videoMessageVo.setCommentCount(Convert.toInt(this.quanZiApi.queryCommentCount(record.getId().toHexString()))); // 评论数

            videoMessageVo.setLikeCount(Convert.toInt(this.quanZiApi.queryLikeCount(record.getId().toHexString())));// 点赞数

            //填充用户信息
            videoMessageVo.setNickname(userInfo1.getNickName());

            videoVoList.add(videoMessageVo);
        }

        //set转list
        ArrayList<VideoMessageVo> videoMessageVos = new ArrayList<>();



        for (VideoMessageVo videoMessageVo : videoVoList) {
            videoMessageVos.add(videoMessageVo);
        }


        ArrayList<VideoMessageVo> videoMessageVoRS = new ArrayList<>();
        for ( int i = (page-1)*pageSize ;i< page*pageSize;i++ ){

            videoMessageVoRS.add(videoMessageVos.get(i));
            if (i >= (videoMessageVos.size() - 1)){
                pageResult.setItems(videoMessageVoRS);
                return pageResult;

            }
        }
        pageResult.setItems(videoMessageVoRS);
        return pageResult;

    }

    private Set<VideoMessageVo> createSet(String sortProp, String sortOrder){
        //创建集合对象
        TreeSet<VideoMessageVo> ts = new TreeSet<>(new Comparator<VideoMessageVo>() {
            @Override
            public int compare(VideoMessageVo o1, VideoMessageVo o2) {
                //o1表示现在要存入的那个元素
                //o2表示已经存入到集合中的元素
                int result = 0;
                if (StringUtils.equalsIgnoreCase(sortProp,"likecount")){
                    //主要条件
                    if (StringUtils.equalsIgnoreCase(sortOrder,"ascending")){
                        result = o1.getLikeCount() - o2.getLikeCount();
                    }else {
                        result = o2.getLikeCount() - o1.getLikeCount();
                    }

                }else if (StringUtils.equalsIgnoreCase(sortProp,"commentcount")){
                    //主要条件
                    if (StringUtils.equalsIgnoreCase(sortOrder,"ascending")){
                        result = o1.getCommentCount() - o2.getCommentCount();
                    }else {
                        result = o2.getCommentCount() - o1.getCommentCount();
                    }

                }

                //次要条件
                result = result == 0 ?  1 : result;
                return result;
            }
        });
        return ts;
    }
}
